package com.enation.app.shop.front.policyInsurance.appController;

import com.enation.app.shop.front.api.inpolicy.entity.InPolicy;
import com.enation.app.shop.front.policyInsurance.adminController.FileEnum.Type1;
import com.enation.app.shop.front.policyInsurance.adminController.FileEnum.Type2;
import com.enation.app.shop.front.policyInsurance.entity.InsuranceCheck;
import com.enation.app.shop.front.policyInsurance.entity.InsuranceFile;
import com.enation.app.shop.front.policyInsurance.entity.PropectRecord;
import com.enation.app.shop.front.policyInsurance.entity.img.Farmerimg;
import com.enation.app.shop.front.policyInsurance.entity.img.Identifyimg;
import com.enation.app.shop.front.policyInsurance.entity.img.Publicityimg;
import com.enation.app.shop.front.policyInsurance.entity.img.Stockimg;
import com.enation.app.shop.front.policyInsurance.service.*;
import com.enation.framework.action.JsonResult;
import com.enation.framework.util.JsonResultUtil;
import org.apache.commons.net.ntp.TimeStamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 保险审核管理
 */
@Scope("prototype")
@Controller
@RequestMapping("/app/insuranceCheck")
public class InPolicyCheckController {
    @Autowired
    private InsuranceCheckMapper insuranceCheckMapper;
    @Autowired
    private InsuranceFileMapper insuranceFileMapper;
    @Autowired
    private InsuranceProtCheckManage insuranceProtCheckManage;
    @Autowired
    private InsuranceProtOrdMapper inPolicyProtOrdManager;
    @Autowired
    private PropectRecordMapper propectRecordMapper;

    /**
     * 获取保险审核信息
     * @param inPolicy_id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getInsuranceCheckInfo")
    public JsonResult getInsuranceCheckInfo(Integer inPolicy_id){
        List list=new ArrayList();
        Map<String,Object> insuranceCheckMap = new HashMap<String, Object>();
        try{
            InsuranceCheck insuranceCheck = insuranceCheckMapper.selectByWarrantyId(inPolicy_id);
            InPolicy inPolicy=inPolicyProtOrdManager.getInPolicyDeatil(inPolicy_id);
            List pactList=new ArrayList();
            List<InsuranceFile> insuranceFileList= insuranceFileMapper.getByInPolicyId(inPolicy_id);
            for(InsuranceFile insuranceFile:insuranceFileList){
                pactList.add(insuranceFile.getUrl());
            }
            insuranceCheck.setDays(365);
            insuranceCheckMap.put("insuranceCheck",insuranceCheck);
            insuranceCheckMap.put("days","365");
            insuranceCheckMap.put("begin_time_def",inPolicy.getBegin_time());
            insuranceCheckMap.put("end_time_def",inPolicy.getEnd_time());
            insuranceCheckMap.put("pactList",pactList);
            return JsonResultUtil.getObjectMessageJson(insuranceCheckMap,"获取信息成功!");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("获取信息失败!");
        }
    }

    /**
     *更新保险审核内容
     * @param record
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateInsuranceCheckInfo")
    public JsonResult updateInsuranceCheckInfo(InsuranceCheck record){
       int a = insuranceCheckMapper.updateByWarrantyId(record);
        if(a==1){
            return JsonResultUtil.getSuccessJson("更新成功!");
        }
        return JsonResultUtil.getErrorJson("更新失败!");
    }

    //o 商业险 1 饲料 2 兽药 4 政策险
    /**
     * 变更保单状态初审通过/不通过
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateUnCheckInPolicy")
    public JsonResult updateUnCheckInPolicy(@RequestBody InsuranceCheck insuranceCheck){
        Map inPolicyMap = new HashMap();
        try {
            if(insuranceCheck.getCheck_status()==0){//1通过，2不通过
                inPolicyMap.put("status",1);
                insuranceCheck.setAuditor_date(new Timestamp(System.currentTimeMillis()));
                insuranceProtCheckManage.updateInPolicyStatus(insuranceCheck.getWarranty_id(),inPolicyMap);
                insuranceCheckMapper.insert(insuranceCheck);
            }
            if(insuranceCheck.getCheck_status()==1){
                if(insuranceCheck.getAuditor_reason().trim()==null){
                    return JsonResultUtil.getErrorJson("拒绝原因不能为空!!");
                }
                inPolicyMap.put("status",2);
                insuranceCheck.setAuditor_date(new Timestamp(System.currentTimeMillis()));
                insuranceProtCheckManage.updateInPolicyStatus(insuranceCheck.getWarranty_id(),inPolicyMap);
                insuranceCheckMapper.insert(insuranceCheck);
            }

        }catch (Exception e){
                e.printStackTrace();
                return JsonResultUtil.getErrorJson("操作失败!");
        }
        return JsonResultUtil.getSuccessJson("操作成功!");
    }



    /**
     * 提交其他信息
     * @param insuranceCheck
     * @return
     */
    @ResponseBody
    @RequestMapping("/addInpolicyCheck")
    public JsonResult addInpolicyCheck(@RequestBody InsuranceCheck insuranceCheck){
        try {
            insuranceCheck.setAuditor_date(new Timestamp(System.currentTimeMillis()));
            insuranceCheckMapper.updateByWarrantyId(insuranceCheck);
            return JsonResultUtil.getSuccessJson("保存成功!");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("保存失败!");
        }
    }

    @ResponseBody
    @RequestMapping("/addPropectRecord")
    public JsonResult addPropectRecord(@RequestBody PropectRecord propectRecord){
        try {
            if(propectRecord.getCheckType()==0){//0通过1不通过
                PropectRecord propectRecord1=new PropectRecord();
                propectRecord1.setCheckType(0);
                propectRecord1.setProspect_result("success");
                propectRecord1.setProspect_date(new Timestamp(System.currentTimeMillis()));
                propectRecord1.setInsured_id(propectRecord.getInsured_id());
                propectRecord1.setProspect_user_id(propectRecord.getProspect_user_id());
                propectRecord1.setProspect_user_name(propectRecord.getProspect_user_name());
                propectRecordMapper.addPropectRecord(propectRecord1);
                if(propectRecord.getIdentifyimgs()!=null){
                    for(Identifyimg identifyimg:propectRecord.getIdentifyimgs()){
                        InsuranceFile insuranceFile=new InsuranceFile();
                        insuranceFile.setType1(Type1.ClaimLoss.value());
                        insuranceFile.setType2(Type2.Identifyimg.value());
                        insuranceFile.setType3(identifyimg.getType3());
                        insuranceFile.setUrl(identifyimg.getUrl());
                        insuranceFile.setInstruction("组织机构代码/身份证照片");
                        insuranceFile.setRelevance_id((long)propectRecord.getInsured_id());
                        insuranceFile.setCreat_at(new Timestamp(System.currentTimeMillis()));
                        insuranceFile.setUpdate_at(new Timestamp(System.currentTimeMillis()));
                        insuranceFileMapper.insert(insuranceFile);
                    }
                }
                if(propectRecord.getStockimgs()!=null){
                    for(Stockimg stockimg:propectRecord.getStockimgs()){
                        InsuranceFile insuranceFile=new InsuranceFile();
                        insuranceFile.setType1(Type1.ClaimLoss.value());
                        insuranceFile.setType2(Type2.Stockimg.value());
                        insuranceFile.setType3(stockimg.getType3());
                        insuranceFile.setUrl(stockimg.getUrl());
                        insuranceFile.setInstruction("栏舍照片");
                        insuranceFile.setRelevance_id((long)propectRecord.getInsured_id());
                        insuranceFile.setCreat_at(new Timestamp(System.currentTimeMillis()));
                        insuranceFile.setUpdate_at(new Timestamp(System.currentTimeMillis()));
                        insuranceFileMapper.insert(insuranceFile);
                    }
                }
                if(propectRecord.getFarmers()!=null){
                    for(Farmerimg farmerimg:propectRecord.getFarmers()){
                        InsuranceFile insuranceFile=new InsuranceFile();
                        insuranceFile.setType1(Type1.ClaimLoss.value());
                        insuranceFile.setType2(Type2.Farmerimg.value());
                        insuranceFile.setType3(farmerimg.getType3());
                        insuranceFile.setUrl(farmerimg.getUrl());
                        insuranceFile.setInstruction("养殖场照片/投保人照片");
                        insuranceFile.setRelevance_id((long)propectRecord.getInsured_id());
                        insuranceFile.setCreat_at(new Timestamp(System.currentTimeMillis()));
                        insuranceFile.setUpdate_at(new Timestamp(System.currentTimeMillis()));
                        insuranceFileMapper.insert(insuranceFile);
                    }
                }
                if(propectRecord.getPublicityimgs()!=null){
                    for(Publicityimg publicityimg:propectRecord.getPublicityimgs()){
                        InsuranceFile insuranceFile=new InsuranceFile();
                        insuranceFile.setType1(Type1.ClaimLoss.value());
                        insuranceFile.setType2(Type2.Publicityimg.value());
                        insuranceFile.setType3(publicityimg.getType3());
                        insuranceFile.setUrl(publicityimg.getUrl());
                        insuranceFile.setInstruction("公示照片");
                        insuranceFile.setRelevance_id((long)propectRecord.getInsured_id());
                        insuranceFile.setCreat_at(new Timestamp(System.currentTimeMillis()));
                        insuranceFile.setUpdate_at(new Timestamp(System.currentTimeMillis()));
                        insuranceFileMapper.insert(insuranceFile);
                    }
                }
                Map map=new HashMap();
                Map map1=new HashMap();
                map1.put("status",4);
                map1.put("update_time",new Timestamp(System.currentTimeMillis()));
                map1.put("receiver_id",propectRecord.getProspect_user_id());
                map1.put("receiver_name",propectRecord.getProspect_user_name());

                map.put("check_status",0);
                map.put("auditor",propectRecord.getProspect_user_name());
                map.put("auditor_id",propectRecord.getProspect_user_id());
                map.put("auditor_date",new Timestamp(System.currentTimeMillis()));
                insuranceProtCheckManage.updateInPolicyStatus(propectRecord.getInsured_id(),map1);
                insuranceCheckMapper.updateByWarrantyId(propectRecord.getInsured_id(),map);
            }
            if(propectRecord.getCheckType()==1){
                if(propectRecord.getProspect_result()==null){
                    return JsonResultUtil.getErrorJson("拒绝原因不能为空!");
                }
                Map map=new HashMap<String, Object>();
                Map map2=new HashMap<String, Object>();
                map.put("status",4);
                map2.put("checkType",1);
                map2.put("auditor_reason",propectRecord.getProspect_result());
                map2.put("receiver_id",propectRecord.getProspect_user_id());
                map2.put("receiver_name",propectRecord.getProspect_user_name());
                insuranceProtCheckManage.updateInPolicyStatus(propectRecord.getInsured_id(),map);
                insuranceCheckMapper.updateByWarrantyId(propectRecord.getInsured_id(),map2);
                propectRecord.setProspect_date(new Timestamp(System.currentTimeMillis()));
                propectRecordMapper.addPropectRecord(propectRecord);
            }
            return JsonResultUtil.getSuccessJson("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("操作失败!");
        }
    }
}
